$(document).ready(function () {

    var successModal=$('#successModal');
    var unknownModal=$('#unknownModal');
    var btn_login=$("#loginSub");


    //获取url中的参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }

    var user_id = getUrlParam("userId");

    console.log("user_id");
    console.log(user_id);

    if (user_id!=""){
        $("#userId").val(user_id);
    }

    $.ajax({
        url: "http://"+urla+"/LoginStatusServlet",
        //url: "./json/categories/parent/0/list.json",
        type: "POST",
        dataType: 'JSON',
        success: function(result){
            console.log(result);
            if (result.status!==1){
                window.location.replace("main.html");
            }
        }
    });

    btn_login.on("click",function (event) {
        event.preventDefault();//使a自带的方法失效
        console.log($('#loginForm').serialize());
        $.ajax({
            type: "POST",
            cache: true,
            url:"http://"+urla+"/LoginServlet",
            data:$('#loginForm').serialize(),
            dataType: "json",
            error: function(request) {
                console.log(request);
                alert("Connection error");
            },
            success: function(data) {
                console.log("data:")
                console.log(data.status);
                if (data.status===0){
                    console.log("successful")
                    successModal.modal('show');
                    successModal.css("padding-right","0");
                    console.log(document.referrer);
                    successModal.on('hide.bs.modal', function () {
                        console.log("document");
                        console.log(document.referrer);
                        self.location=document.referrer;
                    })
                }else if(data.status===1){
                    unknownModal.modal('show');
                    unknownModal.css("padding-right","0");
                    $("#unknownModal .alert").text(data.msg+"请重试");
                    unknownModal.on('hide.bs.modal', function () {
                        location.reload();
                    })
                }
            }
        });
    });


    var InputUsername=$("#username");
    InputUsername.val("");
    var InputUserPsd=$("#password");
    InputUserPsd.val("");
    btn_login.attr("disabled", "disabled");
    btn_login.attr("class", "btn btn-block btn-lg ");

    $(document).bind('input propertychange', function(){
        var user=InputUsername.val();
        var psd=InputUserPsd.val();
        if (user===""||psd==="") {
            btn_login.attr("class", "btn btn-block btn-lg");
            btn_login.attr("disabled","disabled");
        }else {
            btn_login.removeAttr("disabled");
            btn_login.attr("class", "btn  btn-success btn-block btn-lg");
        }
    });


});